package com.demo.androidgame.samgukji_hit;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.KeyEvent;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class MainScreen extends GLScreen{

	Camera2D guiCam;
	SpriteBatcher batcher;
	Rectangle newGame1;
	Rectangle loadGame1;
	Rectangle option1;
	Vector2 touchPoint;
	
	public MainScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 800, 480);
		batcher = new SpriteBatcher(glGraphics, 100);
		newGame1 = new Rectangle(620, 180, 155, 52);
		loadGame1 = new Rectangle(620, 110, 155, 52);
		option1 = new Rectangle(620, 30, 155, 52);
		touchPoint = new Vector2();
	}

	@Override
	public void update(float deltaTime) {
		 List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
	       
	        
	        int len = touchEvents.size();
	        for(int i = 0; i < len; i++) {
	            TouchEvent event = touchEvents.get(i);   
	            if(event.type == TouchEvent.TOUCH_UP) {
	                touchPoint.set(event.x, event.y);
	                guiCam.touchToWorld(touchPoint);
	                
	                if (OverlapTester.pointInRectangle(newGame1, touchPoint)) {
	                	game.setScreen(new SenarioScreen(game));
	                	return;
	                }
	                if (OverlapTester.pointInRectangle(loadGame1, touchPoint)) {
	                	game.setScreen(new ActScreen(game));
	                	return;
	                }
	                if (OverlapTester.pointInRectangle(option1, touchPoint)) {
	                	game.setScreen(new OptionScreen(game));
	                	return;
	                }
	            }
	        }
	        /////////////////////////////////////////////////////////////////////////////
	        List<KeyEvent> keyEvents = game.getInput().getKeyEvents();
			
			int key = keyEvents.size();
			for (int i = 0; i < key; i++) {
				KeyEvent event2 = keyEvents.get(i);
				//if(event2.type == KeyEvent.KEY_DOWN) {
					if (event2.keyCode == android.view.KeyEvent.KEYCODE_BACK) {
						((Activity)game).runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								new AlertDialog.Builder((Activity)game).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Quit").setMessage("Do you want to quit").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
									
									@Override
									public void onClick(DialogInterface dialog, int which) {
										((Activity)game).finish();
										return;
									}
								}).setNegativeButton( "No", null ).show(); 
							}
						});
						
						
					}
				//}
			}
	}

	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(Assets.background);
        batcher.drawSprite(400, 240, 800, 480, Assets.backgroundRegion);
        batcher.endBatch();
       
        
//        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
        
//        batcher.beginBatch(Assets.selectMenu);
//        
//        batcher.drawSprite(650, 480 - 200, 200, 100, Assets.newGame);
//        batcher.drawSprite(650, 480 - 310, 200, 100, Assets.loadGame);
//        batcher.drawSprite(650, 480 - 420, 200, 100, Assets.options);
        
//        batcher.endBatch();
        
        gl.glDisable(GL10.GL_BLEND);
        gl.glDisable(GL10.GL_TEXTURE_2D);
		
	}

	@Override
	public void pause() {
		
		
	}

	@Override
	public void resume() {
		
		
	}

	@Override
	public void dispose() {
		
	}

}
